package com.huike.business.service.impl;


import com.huike.business.domain.TbBusiness;
import com.huike.business.domain.TbBusinessTrackRecord;
import com.huike.business.domain.vo.BusinessTrackVo;
import com.huike.business.mapper.TbBusinessMapper;
import com.huike.business.mapper.TbBusinessTrackRecordMapper;
import com.huike.business.service.ITbBusinessTrackRecordService;
import com.huike.clues.mapper.SysDictDataMapper;
import com.huike.common.utils.SecurityUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 商机跟进记录Service业务层处理
 *
 * @author wgl
 * @date 2021-04-28
 */
@Service
public class TbBusinessTrackRecordServiceImpl implements ITbBusinessTrackRecordService {
    @Autowired
    TbBusinessTrackRecordMapper tbBusinessTrackRecordMapper;
    @Autowired
    TbBusinessMapper tbBusinessMapper;
    @Autowired
    SysDictDataMapper sysDictDataMapper;

    /**
     * 添加商机跟进
     *
     * @param businessTrackVo
     */
    @Override
    @Transactional
    public void add(BusinessTrackVo businessTrackVo) {
        //1.更新商机表成员信息
        TbBusiness tbBusiness = new TbBusiness();
        BeanUtils.copyProperties(businessTrackVo, tbBusiness);
        //更新最后更新时间
        tbBusiness.setId(businessTrackVo.getBusinessId());
        tbBusiness.setUpdateTime(new Date());
        tbBusiness.setStatus("2");
        tbBusinessMapper.updateTbBusiness(tbBusiness);


        //2.添加商机跟进表
        TbBusinessTrackRecord tbBusinessTrackRecord = new TbBusinessTrackRecord();
        BeanUtils.copyProperties(businessTrackVo, tbBusinessTrackRecord);
        tbBusinessTrackRecord.setCreateTime(new Date());
        //更新id
        tbBusinessTrackRecord.setId(null);

        //更新状态
        tbBusinessTrackRecord.setType("0");
        //更新创建人
        String username = SecurityUtils.getUsername();
        tbBusinessTrackRecord.setCreateBy(username);
        tbBusinessTrackRecordMapper.add(tbBusinessTrackRecord);


    }

    /**
     * 查寻商机跟进信息
     *
     * @param id
     * @return
     */
    @Override
    public List<TbBusinessTrackRecord> find(Long id) {
        //根据id查询信息
        List<TbBusinessTrackRecord> list = tbBusinessTrackRecordMapper.find(id);
        List<TbBusinessTrackRecord> tbBusinessTrackRecordList = list.stream().map(tbBusinessTrackRecord ->

                {
                    ArrayList<String> keys = new ArrayList<>();
                    String keyItems = tbBusinessTrackRecord.getKeyItems();
                    if (keyItems != null) {
                        String[] split = keyItems.split(",");
                        for (String s : split) {
                            String label = sysDictDataMapper.selectDictLabel("communication_point", s);
                            keys.add(label);
                        }
                        tbBusinessTrackRecord.setKeys(keys);
                    }

                    return tbBusinessTrackRecord;
                }


        ).collect(Collectors.toList());

        return tbBusinessTrackRecordList;
    }
}
